package com.wkhyapp.lm.view;

import android.graphics.Color;
import android.os.Build;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.loadmore.LoadMoreView;
import com.wkhyapp.lm.R;
import com.wkhyapp.lm.adapter.CollectionAdapter;
import com.wkhyapp.lm.adapter.GoodsAdapter;
import com.wkhyapp.lm.adapter.NoticeAdapter;
import com.wkhyapp.lm.base.SuperActivity;
import com.wkhyapp.lm.contract.CollectionPresenter;
import com.wkhyapp.lm.contract.CollectionView;
import com.wkhyapp.lm.http.vo.Goods;
import com.wkhyapp.lm.http.vo.Notice;
import com.wkhyapp.lm.utils.StatusBarFontUtil;
import com.wkhyapp.lm.weigit.EmptyView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class CollectionActivity extends SuperActivity<CollectionPresenter> implements CollectionView {

    @BindView(R.id.refresh_view)
    SwipeRefreshLayout refresh_view;
    @BindView(R.id.recycler_view)
    RecyclerView recycler_view;
    private CollectionAdapter collectionAdapter;
    private List<Goods> goodses;
    private int mCurrentPage = 1;
    @BindView(R.id.back_iv)
    ImageView back_iv;
    View empty_view;

    @Override
    public void initUI() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            translucentStatusBar();
        }
        StatusBarFontUtil.setLightStatusBarColor(this);
        empty_view = EmptyView.getView(mContext,R.layout.empty_view,R.drawable.wuzhuangtai,"暂无收藏商品");
        goodses = new ArrayList<>();
        collectionAdapter = new CollectionAdapter(R.layout.item_collection_goods,goodses);
        refresh_view.setColorSchemeColors(Color.parseColor("#FFEC48"),Color.parseColor("#FFEC48"));
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recycler_view.setLayoutManager(manager);
        recycler_view.setAdapter(collectionAdapter);
        refresh_view.setRefreshing(false);
        mPresenter.getData();

    }

    @Override
    protected void initListener() {
        collectionAdapter.setLoadMoreView(new LoadMoreView() {
            @Override
            public int getLayoutId() {
                return R.layout.view_index_load_more;
            }

            @Override
            protected int getLoadingViewId() {
                return com.chad.library.R.id.load_more_loading_view;
            }
            @Override
            protected int getLoadFailViewId() {
                return com.chad.library.R.id.load_more_load_fail_view;
            }
            @Override
            protected int getLoadEndViewId() {
                return com.chad.library.R.id.load_more_load_end_view;
            }
        });

        collectionAdapter.setCallBack(new CollectionAdapter.callBack() {
            @Override
            public void action(Goods item) {
                goTo(GoodsInfoActivity.class,item.getId());
            }

            @Override
            public void del(Goods item) {
                showLoad("正在删除");
                mPresenter.del(item.getId());
            }
        });

        collectionAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mCurrentPage = mCurrentPage+1;
                mPresenter.getDataMore(mCurrentPage);

            }
        });
        refresh_view.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mCurrentPage = 1;
                mPresenter.getData();
            }
        });

        back_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    @Override
    public void setGoods(List<Goods> list) {
        goodses.clear();
        if(list!=null&&list.size()>0){
            goodses.addAll(list);
            collectionAdapter.removeFooterView(empty_view);
        }else{
            collectionAdapter.removeFooterView(empty_view);
            collectionAdapter.addFooterView(empty_view);
        }
        collectionAdapter.notifyDataSetChanged();
        refresh_view.setRefreshing(false);
        collectionAdapter.loadMoreComplete();
    }

    @Override
    public void setGoodsMore(List<Goods> list) {
        if(list.size()==0){
            collectionAdapter.loadMoreComplete();
            collectionAdapter.loadMoreEnd();
        }else {
            goodses.addAll(list);
            collectionAdapter.notifyDataSetChanged();
            collectionAdapter.loadMoreComplete();
        }
    }

    @Override
    public void succ() {
        TToast("删除成功");
        dismissLoad();
        mPresenter.getData();
    }

    @Override
    protected CollectionPresenter createPresenter() {
        return mPresenter = new CollectionPresenter(this);
    }

    @Override
    protected void afterCreate() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_collection;
    }

}
